<template>
	<view>
		<u-field v-model="state.form.phone" label="手机号" placeholder="请输入手机号"> </u-field>
		<u-field v-model="state.form.password" label="密码" password placeholder="请输入密码"></u-field>
		<view class="u-padding-top-20">
			<u-button type="primary" shape="circle" @click="login">登录</u-button>
		</view>
	</view>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { useUserStore } from '@/stores/user';
import { Local } from '@/utils/storage';
const storesUserStore = useUserStore();
const state = reactive<{ form: { phone: string; password: string } }>({
	form: {
		phone: '',
		password: '',
	},
});

const login = () => {
	if (!state.form.phone) {
		return uni.$u.toast('请输入手机号');
	}
	if (!uni.$u.test.mobile(state.form.phone)) {
		return uni.$u.toast('手机号格式错误');
	}
	if (!state.form.password) {
		return uni.$u.toast('请输入密码');
	}
	storesUserStore.passwordLogin(state.form).then((response) => {
		uni.$u.toast('登录成功');
		//获取用户信息
		storesUserStore.getInfo();
		uni.$hxRouter.push({ type: 'reLaunch', url: Local.get('redirectUrl') ? Local.get('redirectUrl') : '/pages/home/index' });
		Local.remove('redirectUrl');
	});
};
</script>

<style></style>
